﻿using OrderSystem.Common;
using OrderSystem.DataAccess;
using OrderSystem.SystemFramework;
using OrderSystem.Templet;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace OrderSystem
{
    public partial class FormUpdateProject : FormTemplet
    {
        private int _nProjectID = -1;

        private int _nCustomerID = -1;

        public FormUpdateProject()
        {
            this.InitializeComponent();
        }

        public FormUpdateProject(int nProjectID) : this()
        {
            this._nProjectID = nProjectID;
            DataTable dataTable = null;
            Projects projects = new Projects();
            try
            {
                dataTable = projects.LoadProject(this._nProjectID);
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            catch (Exception ex2)
            {
                MessageBox.Show(ex2.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            projects.Dispose();
            if (dataTable != null)
            {
                if (dataTable.Rows.Count > 0)
                {
                    this._nCustomerID = Convert.ToInt32(dataTable.Rows[0][1]);
                    this.txtCustomer.Text = dataTable.Rows[0][2].ToString();
                    this.txtProjectName.Text = dataTable.Rows[0][3].ToString();
                    this.txtLinkman.Text = dataTable.Rows[0][4].ToString();
                    this.txtPhone.Text = dataTable.Rows[0][5].ToString();
                    this.txtAddress.Text = dataTable.Rows[0][6].ToString();
                    this.txtRemark.Text = dataTable.Rows[0][7].ToString();
                    this.btnOK.Enabled = true;
                }
                else
                {
                    MessageBox.Show("数据库里没有查到此 计划 的数据！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                dataTable.Dispose();
            }
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            CustomerData customerData = FormSelectCustomer.ShowBox();
            if (customerData != null)
            {
                this._nCustomerID = customerData.CustomerID;
                this.txtCustomer.Text = customerData.CompanyName + "(" + customerData.Linkman + ")";
            }
            this.txtProjectName.Focus();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (!LoginInfo.Role[PowerName.Project_Update])
            {
                MessageBox.Show("你没有操作这里的权限！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                this.txtProjectName.Text = CommonMethod.Fun1(this.txtProjectName.Text);
                this.txtAddress.Text = CommonMethod.Fun1(this.txtAddress.Text);
                this.txtLinkman.Text = CommonMethod.Fun1(this.txtLinkman.Text);
                this.txtRemark.Text = CommonMethod.Fun1(this.txtRemark.Text);
                if (this.txtCustomer.Text.Trim() == "")
                {
                    MessageBox.Show("请选择客户", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    this.btnSelect.Focus();
                }
                else if (this.txtProjectName.Text.Trim() == "" || this.txtProjectName.Text.Trim().Length > 32)
                {
                    MessageBox.Show("请填写计划名称 或 你的名称超过了32个字符", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    this.txtProjectName.Focus();
                }
                else if (this.txtLinkman.Text.Trim() == "" || this.txtLinkman.Text.Trim().Length > 16)
                {
                    MessageBox.Show("联系人 应在 1-16 个字符", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    this.txtLinkman.Focus();
                }
                else
                {
                    if (this.txtPhone.Text.Trim() != "")
                    {
                        if (this.txtPhone.Text.Trim().Length > 32 || this.txtPhone.Text.Trim().Length < 6)
                        {
                            MessageBox.Show("如果填写电话号码 应该在6-32个字符之间", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            this.txtPhone.Focus();
                            return;
                        }
                    }
                    if (this.txtAddress.Text.Trim() == "" || this.txtAddress.Text.Trim().Length > 64)
                    {
                        MessageBox.Show("请填写计划地址 或 你的地址超过了64个字符", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        this.txtAddress.Focus();
                    }
                    else if (this.txtRemark.Text.Trim().Length > 64)
                    {
                        MessageBox.Show("备注 应该在64个字符之内", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        this.txtRemark.Focus();
                    }
                    else
                    {
                        Projects projects = new Projects();
                        bool flag = false;
                        try
                        {
                            flag = projects.UpdateProject(this._nProjectID, this._nCustomerID, this.txtProjectName.Text.Trim(), this.txtLinkman.Text.Trim(), this.txtPhone.Text.Trim(), this.txtAddress.Text.Trim(), this.txtRemark.Text.Trim());
                        }
                        catch (SqlException ex)
                        {
                            int number = ex.Number;
                            if (number != 2627)
                            {
                                MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            }
                            else
                            {
                                MessageBox.Show("此计划已经存在，不能给同一个客户添加相同的计划！\r\n\r\n你可以继续使用已经存在的这个计划 或者 请更换一个计划名称在试一下", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            }
                        }
                        catch (Exception ex2)
                        {
                            MessageBox.Show(ex2.Message, "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                        projects.Dispose();
                        if (flag)
                        {
                            MessageBox.Show("计划 修改 成功！", "Successed", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                            base.Close();
                        }
                    }
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            base.Close();
        }
    }
}
